﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HotCopperUserScanService.Tests
{
    [TestClass]
    public class PostsDatabaseTests
    {
        [TestMethod]
        public void InsertNewPostFromListOfPosts()
        {
            PostsDatabase.InMemory = true;
            PostsDatabase.DeleteAll();

            var posts = new List<Post>()
            {
                new Post() { DateTime = new DateTime(2014, 1, 27, 3, 1, 12), Content = "Content 1", Username = "TB"},
                new Post() { DateTime = new DateTime(2014, 1, 27, 3, 2, 13), Content = "Content 2", Username = "TB"}
            };

            PostsDatabase.Insert(posts);

            var retrievedPosts = PostsDatabase.Retrieve("TB");
            Assert.IsTrue(retrievedPosts.Any());

            PostsDatabase.Delete(retrievedPosts);

            var isDeletedPosts = PostsDatabase.Retrieve("TB");
            Assert.IsFalse(isDeletedPosts.Any());

            PostsDatabase.CloseDown();
        }

        [TestMethod]
        public void RetrieveMaxItems()
        {
            PostsDatabase.InMemory = true;
            PostsDatabase.DeleteAll();

            var posts = new List<Post>()
            {
                new Post() { DateTime = new DateTime(2013, 1, 27, 3, 1, 12), Content = "Content 1", Username = "TB"},
                new Post() { DateTime = new DateTime(2013, 1, 28, 3, 2, 13), Content = "Content 2", Username = "TB"},
                new Post() { DateTime = new DateTime(2013, 1, 29, 3, 1, 12), Content = "Content 1", Username = "TB"},
                new Post() { DateTime = new DateTime(2013, 1, 30, 3, 2, 13), Content = "Content 2", Username = "TB"}
            };

            PostsDatabase.Insert(posts);

            var retrievedPosts = PostsDatabase.Retrieve("TB", 2);
            Assert.AreEqual(2, retrievedPosts.Count());

            PostsDatabase.CloseDown();
        }
    }
}